<template>
  <div class="page">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="服务器名称/IP" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      ref="dataTreeList"
      v-loading="dataListLoading"
      row-key="id"
      @expand-change="showLogFileInfo"
      style="width: 100%;">
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label=""
        width="50"
        type="expand">
         <template slot-scope="props">
          <div style="border:1px solid #ebeef5;padding:15px;">
            <div v-if="props.row.modules&&props.row.modules.length" style="display: flex;width:100%; height:30px;border-bottom:1px solid #ebeef5;">
              <div style="width:180px;text-align: center;">模块名称</div>
              <div style="width:100px;text-align: center;">模块类型</div>
              <div style="width:220px;text-align: center;">日志名称</div>
              <div style="width:480px;text-align: center;">日志路径</div>
              <div style="width:180px;text-align: center;">文件大小</div>
              <div style="width:180px;text-align: center;">更新时间</div>
              <div style="width:180px;text-align: center;">操作</div>
            </div>
            <div v-for="(module , mkey) in props.row.modules">
              <div v-if="module.logs" >
                <div v-for="(log,lkey) in module.logs" style="display: flex;width:100%; height:40px;line-height: 40px;">
                   <div style="width:180px;text-align: center;">{{module.name}}</div>
                   <div style="width:100px;text-align: center;">
                      <el-tag v-if="module.moduleType === 'springboot'" size="small" type="" effect="dark">业务服务</el-tag>
                      <el-tag v-if="module.moduleType === 'gateway'" size="small" type="success" effect="dark">网关</el-tag>
                      <el-tag v-if="module.moduleType === 'eureka'" size="small" type="success" effect="dark">注册中心</el-tag>
                   </div>
                   <div style="width:220px;text-align: center;">{{log.nodeName}}</div>
                   <div style="width:480px;text-align: center;">{{log.path}}</div>
                   <div style="width:180px;text-align: center;">{{log.attributes.size}}</div>
                   <div style="width:180px;text-align: center;">{{log.updateDateText}}</div>
                   <div style="width:180px;text-align: center;">
                      <el-button type="text" size="small" @click="showLogContent(module.serverId,log.path)">查看</el-button>
                      <el-button type="text" size="small" @click="clearlog(module.serverId,log.path)">清空</el-button>
                   </div>
                </div>
              </div>
              <div  v-else  >
                <div style="display: flex;height:40px;line-height: 40px;">
                    <div style="width:180px;">{{module.name}}</div>
                   <div style="width:100px;">
                      <el-tag v-if="module.moduleType === 'springboot'" size="small" type="" effect="dark">业务服务</el-tag>
                      <el-tag v-if="module.moduleType === 'gateway'" size="small" type="success" effect="dark">网关</el-tag>
                      <el-tag v-if="module.moduleType === 'eureka'" size="small" type="success" effect="dark">注册中心</el-tag>
                   </div>
                   <div style="width:220px;text-align: center;">---</div>
                   <div style="width:480px;text-align: center;">---</div>
                   <div style="width:180px;text-align: center;">---</div>
                   <div style="width:180px;text-align: center;">---</div>
                   <div style="width:180px;text-align: center;">---</div>
                </div>
              </div>
            </div>
          </div>
         </template>
      </el-table-column>
      <el-table-column
        prop="projectName"
        header-align="center"
        align="center"
        label="项目名称">
      </el-table-column>
      <el-table-column
        prop="codeAddress"
        header-align="center"
        align="center"
        label="代码地址">
      </el-table-column>
      <el-table-column
        prop="framework"
        header-align="center"
        align="center"
        width="100"
        label="开发框架">
        <template slot-scope="scope">
        <icon-svg  :name="scope.row.framework"></icon-svg>
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        width="100"
        label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" size="small" type="danger">禁用</el-tag>
          <el-tag v-else size="small">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        width="180"
        label="创建时间">
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <runtime-log v-if="runtimeLogVisible" ref="runtimeLog"></runtime-log>
  </div>
</template>

<script>
  import runtimeLog from '../logs/runtime-log'
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        selecteditem:'',
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        runtimeLogVisible:false
      }
    },
    components: {
      runtimeLog
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/backend/treelist'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
            if(this.selecteditem!=''){
              this.$http({
                url: this.$http.adornUrl('/backend/treelog'),
                method: 'get',
                params: this.$http.adornParams({id:this.selecteditem.id})
              }).then(({data}) => {
                if (data && data.code === 0) {
                  for(var i in this.dataList){
                    if(this.dataList[i].id === this.selecteditem.id){
                        this.dataList[i].modules=data.list
                    }
                  }
                }
                this.dataListLoading = false
              })
            }
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      showLogContent(serverId,logPath){
          this.runtimeLogVisible = true
          this.$nextTick(() => {
            this.$refs.runtimeLog.init(serverId,logPath)
          })
      },
      clearlog(serverId,logPath){
        this.$confirm(`确定对[${logPath}]进行清除操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/backend/clearLog'),
            method: 'post',
            params: this.$http.adornParams({serverId:serverId,logPath:logPath})
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      },
      showLogFileInfo(item,rows){
        if(this.selecteditem!=''){
            this.$refs.dataTreeList.toggleRowExpansion(this.selecteditem, false);
        }
        if(rows.length>0){
            this.selecteditem=item;
            this.$http({
              url: this.$http.adornUrl('/backend/treelog'),
              method: 'get',
              params: this.$http.adornParams({id:item.id})
            }).then(({data}) => {
              if (data && data.code === 0) {
                item.modules=data.list
                console.log(this.dataList)
              }
              this.dataListLoading = false
            })
        }else{
            this.selecteditem=''
        }
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var delIds = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${delIds.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/backend/delete'),
            method: 'post',
            data: this.$http.adornData(delIds, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      }
    }
  }
</script>
